public class test09 {
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
        public int minDepth(TreeNode root) {
            //只能用迭代法！
            //找到左右节点都不存在处为止！
            if(root==null){
                return 0;
            }
            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root);
            int height = 0;
            while(!queue.isEmpty()){
                int len = queue.size();
                for(int i = 0;i<len;i++){
                    TreeNode tmp = queue.poll();
                    if(tmp.left!=null){
                        queue.offer(tmp.left);
                    }
                    if(tmp.right!=null){
                        queue.offer(tmp.right);
                    }
                    if(tmp.left==null && tmp.right==null){
                        //直接结束
                        return height+1;
                    }
                }
                height++;
            }
            return height+1;
        }
}
